package initx

import (
	"back/conf"
	"back/constant"
	"back/entity/nailEntity"
	"github.com/pelletier/go-toml/v2"
	"gorm.io/gorm"
	"os"
	"utilx/logx"
)

// toml ==> yaml
func InitConf() conf.ServerConf {
	var config conf.ServerConf
	//os.Open("conf.toml")
	err := toml.Unmarshal(conf.Config, &config) // conf.Config
	if err != nil {
		panic(err)
	}
	//
	if _, err := os.Stat("nail.toml"); err == nil {
		file, err := os.ReadFile("nail.toml")
		if err != nil {
			panic(err)
		}
		err = toml.Unmarshal(file, &config) // conf.Config
		if err != nil {
			panic(err)
		}
		getwd, err := os.Getwd()
		if err != nil {
			panic(err)
		}
		logx.Debug("getwd:", getwd)
		logx.Debugf("Find nail.toml\nconfig:%+v", config)
	}
	constant.Config = config

	return config
}

func InitDBModel(db *gorm.DB) {
	err := db.AutoMigrate(
		nailEntity.NailMember{},
		nailEntity.NailMemberCard{},
		nailEntity.NailMemberCardLog{},
		nailEntity.NailProduct{},
		nailEntity.NailProject{},
		nailEntity.NailService{},
		nailEntity.NailStorageRecord{},
	)
	if err != nil {
		panic(err)
	}
}
